package com.ruoyi.archives.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.archives.domain.SysStudywork;
import com.ruoyi.archives.utile.DirectorRoleUtile;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UuidUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.archives.domain.SysTeacherStudywork;
import com.ruoyi.archives.service.ISysTeacherStudyworkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教师工作教育经历Controller
 * 
 * @author gzhankh
 * @date 2022-03-06
 */
@Api("教师工作教育经历接口")
@RestController
@RequestMapping("/archives/teacher_studywork")
public class SysTeacherStudyworkController extends BaseController
{
    @Autowired
    private ISysTeacherStudyworkService sysTeacherStudyworkService;


    @Autowired
    private DirectorRoleUtile directorRoleUtile;
    /**
     * 查询教师工作教育经历列表
     */
    @ApiOperation("查询教师工作教育经历列表")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTeacherStudywork sysTeacherStudywork)
    {
        startPage();
        List<SysTeacherStudywork> list = sysTeacherStudyworkService.selectSysTeacherStudyworkList(sysTeacherStudywork);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        list = list.stream().sorted(Comparator.comparing(SysTeacherStudywork::getStartTime).reversed()).collect(Collectors.toList());
        int stDirectorRole = directorRoleUtile.getSTDirectorRole(loginUser);

        if(!SecurityUtils.isAdmin(loginUser.getUserId())&&stDirectorRole==1){
            List<SysTeacherStudywork> studyWorks = new ArrayList<>();
            for (SysTeacherStudywork studyWork : list) {
                if(studyWork.getArcNum().equals(loginUser.getUserId().toString())){
                    studyWorks.add(studyWork);
                }
            }
            studyWorks = studyWorks.stream().sorted(Comparator.comparing(SysTeacherStudywork::getStartTime).reversed()).collect(Collectors.toList());
            return getDataTable(studyWorks);
        }else {
            return getDataTable(list);
        }
    }

    /**
     * 导出教师工作教育经历列表
     */
    @ApiOperation("导出教师工作教育经历列表")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:export')")
    @Log(title = "教师工作教育经历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTeacherStudywork sysTeacherStudywork)
    {
        List<SysTeacherStudywork> list = sysTeacherStudyworkService.selectSysTeacherStudyworkList(sysTeacherStudywork);
        ExcelUtil<SysTeacherStudywork> util = new ExcelUtil<SysTeacherStudywork>(SysTeacherStudywork.class);
        util.exportExcel(response, list, "教师工作教育经历数据");
    }

    /**
     * 获取教师工作教育经历详细信息
     */
    @ApiOperation("获取教师工作教育经历详细信息")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sysTeacherStudyworkService.selectSysTeacherStudyworkById(id));
    }

    /**
     * 新增教师工作教育经历
     */
    @ApiOperation("新增教师工作教育经历")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:add')")
    @Log(title = "教师工作教育经历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTeacherStudywork sysTeacherStudywork)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysTeacherStudywork.setArcNum(loginUser.getUserId().toString());
        sysTeacherStudywork.setId(UuidUtils.get8UUID());
        sysTeacherStudywork.setUserName(loginUser.getUsername());
        return toAjax(sysTeacherStudyworkService.insertSysTeacherStudywork(sysTeacherStudywork));
    }

    /**
     * 修改教师工作教育经历
     */
    @ApiOperation("修改教师工作教育经历")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:edit')")
    @Log(title = "教师工作教育经历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTeacherStudywork sysTeacherStudywork)
    {
        return toAjax(sysTeacherStudyworkService.updateSysTeacherStudywork(sysTeacherStudywork));
    }

    /**
     * 删除教师工作教育经历
     */
    @ApiOperation("删除教师工作教育经历")
    @PreAuthorize("@ss.hasPermi('archives:teacher_studywork:remove')")
    @Log(title = "教师工作教育经历", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysTeacherStudyworkService.deleteSysTeacherStudyworkByIds(ids));
    }
}
